package com.hydl.power.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具类
 *
 * @author ZhangBaiQiang
 */
public class DateTimeUtils extends DataSourceAutoConfiguration {

    private static Logger log = LoggerFactory.getLogger(DateTimeUtils.class);

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYY_MM = "yyyy-MM";

    private static SimpleDateFormat sdf_long = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf_short = new SimpleDateFormat("yyyy-MM-dd");


    /**
     * String类型日期转换为Date类型
     *
     * @param datetime
     * @return
     * @throws ParseException
     */
    public static Date strToDate(String datetime) {
        try {
            return sdf_long.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author jwc
     * @description 获得两时间相差毫秒值
     */
    public static long getTimeDifference(Date now, Date date) {
        try {
            //获取时间差
            return now.getTime() - date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 日期转指定格式的字符串，"yy-MM-dd HH:mm:ss"
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String getLongStrDate() {
        return String.valueOf(System.currentTimeMillis());
    }

    public String getFormat(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return dateFormat.format(date);
    }

    public String getFormats(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        return dateFormat.format(date);
    }

    public String getFormatYM(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM);
        return dateFormat.format(date);
    }

    //获取当前系统前三个月信息
    public static String getUpMothDate() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(Calendar.MONTH, -3);//减一个月
        str = sdf.format(lastDate.getTime());
        return str;
    }

}
